﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Generic;
using GeekShop.Properties;
using GeekShop.Tools;

namespace GeekShop
{
    public partial class MainForm : MenuManager
    {
        #region Fields
        private FileTypeManager fileTypeManager = new FileTypeManager();
        private PictureManager pictureManager = new PictureManager();
        private ToolManager toolManager;
        #endregion

        #region Constructor
        public MainForm()
        {
            InitializeComponent();
            InitializeMenuManager();

            this.pictureManager.MdiParent = this;
            this.MdiChildActivate += new System.EventHandler(this.MdiChildActivated);
        }
        #endregion

        #region Button Event Methods
        private void itemLoad_Click(object sender, EventArgs e)
        {
            if (this.fileTypeManager.OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.pictureManager.Load(this.fileTypeManager.OpenFileDialog.FileName);
            }
        }
        private void itemSaveAs_Click(object sender, EventArgs e)
        {
            if (!this.pictureManager.Empty)
            {
                this.fileTypeManager.SaveFileDialog.FileName = this.pictureManager.SelectedPicture.Path.Substring(this.pictureManager.SelectedPicture.Path.LastIndexOf('\\') + 1);
                if (this.fileTypeManager.SaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    EncoderParameters parameters = new EncoderParameters(1);
                    parameters.Param[0] = new EncoderParameter(
                        System.Drawing.Imaging.Encoder.Quality,
                        100L);

                    this.pictureManager.SelectedPicture.SaveBitmapAs(
                        this.fileTypeManager.SaveFileDialog.FileName,
                        this.fileTypeManager.GetSelectedImageEncoderInfo(),
                        parameters);
                }
            }
        }
        private void itemArrangeForms_Click(object sender, EventArgs e)
        {
            this.LayoutMdi((MdiLayout)((ToolStripItem)sender).Tag);
        }
        private void itemUndo_Click(object sender, EventArgs e)
        {
            this.pictureManager.SelectedPicture.UndoCommand();
        }
        private void itemRedo_Click(object sender, EventArgs e)
        {
            this.pictureManager.SelectedPicture.RedoCommand();
        }
        #endregion

        #region Event Methods
        public void MdiChildActivated(object sender, EventArgs e)
        {
            if (this.pictureManager.SelectedPicture != null)
            {
                this.listLayers.ItemChecked -= new ItemCheckedEventHandler(this.listLayers_ItemChecked);
                this.listLayers.Items.Clear();

                for (int i = 0; i < this.pictureManager.SelectedPicture.Layers.Count; i++)
                {
                    Layer layer = this.pictureManager.SelectedPicture.Layers[i];
                    ListViewItem item = new ListViewItem(new string[3] { "", i.ToString(), layer.Name })
                    {
                        Tag = layer,
                        Checked = layer.Visible
                    };
                    this.listLayers.Items.Add(item);
                }

                this.listLayers.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.listLayers.ItemChecked += new ItemCheckedEventHandler(this.listLayers_ItemChecked);
            }
        }    
        private void listLayers_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            ((Layer)e.Item.Tag).Visible = e.Item.Checked;
            this.pictureManager.SelectedPicture.Form.Refresh();
        }
        private void listLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listLayers.SelectedItems.Count > 0)
            {
                this.pictureManager.SelectedPicture.SelectedLayer = (Layer)this.listLayers.SelectedItems[0].Tag;
            }
        }
        #endregion
    }
}
